package cn.service;

import cn.pojo.Bus;
import cn.pojo.Car;
import cn.pojo.Vehicle;
import cn.util.DateUtil;

import java.util.*;

/**
 * @author
 * @description
 * @date 2021/8/29 10:41
 */
public class Manager {
    //1、存放车辆信息的集合 key车牌  value汽车对象
    Map<String, Vehicle> cars = new HashMap<String, Vehicle>();

    //2、车辆入场
    public void vehicleIn() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入车辆类型：1.轿车  2.客车");
        String type = scanner.next(); //类型
        System.out.println("请输入车牌号：");
        String no = scanner.next();  //车牌
        if (type.equals("1")) {
            Car car = new Car(type, no, new Date(), 1);
            cars.put(no, car);
        } else {
           // String sj="2021-8-28 15:37:20";
            Bus bus = new Bus(type, no, new Date(), 1);
            cars.put(no, bus);
        }
        System.out.println("入场成功！");
    }

    //3、车辆离场
    public void vehicleOut() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入车牌号：");
        String no = scanner.next();  //车牌
        System.out.println("请输入离场时间");
        String outTime = scanner.next();
        Vehicle vehicle = cars.get(no);
        if(vehicle instanceof Car){
            Car car=(Car)vehicle;
            car.setCarStatus(0);
            car.setOutTime(DateUtil.toDate(outTime));
            //毫秒数 两个时间差的分钟数
            long i = car.getOutTime().getTime() - car.getInTime().getTime();
            car.setTotalTime(i);
            double cale = car.cale(car.getTotalTime());
            System.out.println("共需要支付："+cale);
            System.out.println("您需要打印小票吗?y/n");
        }
        else{
            Bus bus=(Bus)vehicle;
            bus.setCarStatus(0);
            bus.setOutTime(DateUtil.toDate(outTime));
            long i = bus.getOutTime().getTime() - bus.getInTime().getTime();//毫秒数 --两个时间差的分钟数
            bus.setTotalTime(i);
            double cale = bus.cale(bus.getTotalTime());
            System.out.println("共需要支付："+cale);
            System.out.println("您需要打印小票吗?y/n");
        }
        System.out.println("离场成功！");
    }

    //4 查看所有车辆 根据状态分开查询  1 在场  0 离场
    public void showVehicle(){
        System.out.println("车牌号\t车辆类型\t入场时间\t\t\t离场时间\t停车时长\t车辆状态");
        for (String no : cars.keySet()) {
            Vehicle vehicle = cars.get(no);
            String carType = vehicle.getCarType();
            if(carType.equals("1")){
                carType="轿车";
            }else {
                carType="客车";
            }
            Date inTime = vehicle.getInTime();
            String rcsj = DateUtil.toString(inTime);

            int carStatus = vehicle.getCarStatus();
            String status=null;
            if(carStatus==1){
                status="在场";
            }else {
                status="离场";
            }

            Date outTime = vehicle.getOutTime();
            String lcsj="";
            if (outTime==null){
                lcsj="未离场";
            }

            System.out.println(vehicle.getCarNo()+"\t"+carType+"\t"+rcsj+"\t\t\t"+lcsj+"\t"+vehicle.getTotalTime()+"\t\t"+status);

        }
    }

}
